package com.hyphenate.easeui.tuison;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.RemoteViews;

import androidx.core.app.NotificationCompat;


import com.hyphenate.easeui.R;

import java.net.URISyntaxException;
import java.util.List;

import static android.content.Context.NOTIFICATION_SERVICE;

public class NotificationUtils {
    private static NotificationUtils instance;
    private NotificationManager manager;

    public static NotificationUtils getInstance(){
        if(instance == null){
            instance =  new NotificationUtils();
        }
        return instance;
    }

    public void showNotification(Context context, String id, String title, String text){
        if(manager == null){
            manager =  (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        }
        if(!isBackground(context)){
            return;
        }
        Notification notification = new NotificationCompat.Builder(context)
                .setContentTitle(title)  //设置标题
                .setContentText(text) //设置内容
                .setWhen(System.currentTimeMillis())  //设置时间
                .setSmallIcon(R.drawable.ic_launcher)  //设置小图标
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher)).build();
        manager.notify(1,notification);
    }

    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    Log.i(context.getPackageName(), "后台"
                            + appProcess.processName);
                    return true;
                } else {
                    Log.i(context.getPackageName(), "前台"
                            + appProcess.processName);
                    return false;
                }
            }
        }
        return false;
    }
}
